//
//  LBPageControl.m
//  MXClassroom
//
//  Created by 你个LB on 16/9/9.
//  Copyright © 2016年 你个LB. All rights reserved.
//

#import "LBPageControl.h"

@interface LBPageControl ()
{
    NSInteger _lastPage;
}

@end

@implementation LBPageControl

- (void)setNumberOfPages:(NSInteger)numberOfPages{
    
    int space = 3;
    
    int pageViewWidth = 9;
    int pageViewHeight = 3;
    
    for (int i = 0; i < numberOfPages; i ++) {
        UIView * pageView = [[UIView alloc] initWithFrame:CGRectMake(i * pageViewWidth + space * (i + 1), 0, pageViewWidth, pageViewHeight)];
        pageView.backgroundColor = [UIColor whiteColor];
        [self addSubview:pageView];
        if (i == 0) {
            pageView.backgroundColor = [UIColor blackColor];
        }
    }
}

- (void)setCurrentPage:(NSInteger)currentPage{
    
    if (_currentPage != currentPage) {
        _currentPage = currentPage;
        NSArray * subviews = self.subviews;
        if (currentPage == 0) {
            _lastPage = 3;
        } else{
            _lastPage = currentPage - 1;
        }
        
       // 上一个试图
        UIView * lastPageView = subviews[_lastPage];
        lastPageView.backgroundColor = [UIColor whiteColor];
        
        // 当前试图
        UIView * currentPageView = subviews[currentPage];
    
        currentPageView.backgroundColor = [UIColor blackColor];
    }
}


@end
